package com.ego.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ego.admin.dao.SysOperationLogDao;
import com.ego.admin.po.SysOperationLog;
import com.ego.admin.service.SysOperationLogService;
import com.ego.admin.vo.SysOperationSearchVo;
import com.ego.common.exception.GlobalException;
import com.ego.common.util.PageUtil;
import com.ego.common.vo.PageVo;
import org.omg.CORBA.SystemException;
import org.springframework.stereotype.Service;

/**
 * @program: ego
 * @description: 系统操作日志服务实现类
 * @author: KamTang
 * @create: 2023-05-02 09:40
 **/
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogDao, SysOperationLog> implements SysOperationLogService {
    @Override
    public PageVo<SysOperationLog> search(SysOperationSearchVo vo) {
        LambdaQueryWrapper<SysOperationLog> query = Wrappers.<SysOperationLog>lambdaQuery()
                .like(vo.getKeyword() != null, SysOperationLog::getUsername, vo.getKeyword())
                .or().like(vo.getKeyword() != null, SysOperationLog::getOperation, vo.getKeyword())
                .orderByAsc(SysOperationLog::getCreateTime);
        Page<SysOperationLog> page = baseMapper.selectPage(Page.of(vo.getPageNum(), vo.getPageSize()), query);
        return PageUtil.toPage(page);
    }

    @Override
    public void saveLog(SysOperationLog log) {
        this.baseMapper.insert(log);
    }

    @Override
    public void deleteBatch(Long[] ids) throws SystemException {
        for (Long id : ids) {
            SysOperationLog sysLogEntity = this.baseMapper.selectById(id);
            if (sysLogEntity == null) {
                throw new GlobalException("id=" + id + "操作日志不存在", 500);
            }
            this.baseMapper.deleteById(id);
        }
    }
}
